from params import *
from utils import define_print

BASES = {
    'EBP': 0b00,
    'Param1': 0b01,
    'Param2': 0b10,
    'Param3': 0b11
}


def absolute_addr(addr: int):
    """build parameter absolute address"""
    # high 1 bit
    res = 0
    # low 14 bits
    res += (addr & 0x3fff)
    assert 0 <= res < 0xffff
    return res


def relative_addr(which_base, bias: int, plus_or_sub: bool):
    """
    build parameter relative address
    :param which_base:
    :param bias:
    :param plus_or_sub: True for base_addr plus bias or False for sub
    :return:
    """
    # high 1 bit
    res = 0b1 << 15

    res += BASES[which_base] << 13

    if not plus_or_sub:
        res += 1 << 12

    res += bias
    assert 0 <= bias < pow(2, 13)

    assert 0 <= res < 0xffff

    return res


def jmp_addr(bias: int, plus_or_sub: bool):
    """build jmp address"""
    res = 0
    if not plus_or_sub:
        res = 1 << 15
    res += bias
    assert 0 <= bias < pow(2, 12)
    return res


def gen_bases():
    for name, value in BASES.items():
        define_print(f'{name}_ID', f'2\'h{value:x}')


if __name__ == "__main__":
    gen_bases()
